Welcome to the ggplot2 graph gallery! This Quarto website contains a series of graphs composed in ggplot2.

Install ggplot2 using the code below:

View code
install.packages("ggplot2")
library(ggplot2)

Or install ggplot2 as part of the tidyverse:

View code
install.packages("tidyverse")
library(tidyverse)

Data

So many penguins!

To improve reproducibility, the majority of the graphs are built using the palmerpenguins::penguins data.

penguins

Artwork by @allison_horst

Install this package using the code below:

View code
install.packages("palmerpenguins")
library(palmerpenguins)
palmerpenguins::penguins |> glimpse()
Rows: 344
Columns: 8
$ species           <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
$ island            <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
$ bill_length_mm    <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
$ bill_depth_mm     <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
$ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
$ body_mass_g       <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
$ sex               <fct> male, female, female, NA, female, male, female, male…
$ year              <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…

Some of the graphs use datasets from the fivethirtyeight package.

To view a table of available datasets in the fivethirtyeight package, view the Data Frame Name and Article Title columns in the datasets_master table.

View code
install.packages("fivethirtyeight")
library(fivethirtyeight)
fivethirtyeight::datasets_master |> 
    select(`Data Frame Name`, `Article Title`)

Additional datasets

movies

A few of the graphs are built using the ggplot2movies::movies data.

View code
install.packages("ggplot2movies")
library(ggplot2movies)
ggplot2movies::movies |> glimpse()
Rows: 58,788
Columns: 24
$ title       <chr> "$", "$1000 a Touchdown", "$21 a Day Once a Month", "$40,0…
$ year        <int> 1971, 1939, 1941, 1996, 1975, 2000, 2002, 2002, 1987, 1917…
$ length      <int> 121, 71, 7, 70, 71, 91, 93, 25, 97, 61, 99, 96, 10, 10, 10…
$ budget      <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ rating      <dbl> 6.4, 6.0, 8.2, 8.2, 3.4, 4.3, 5.3, 6.7, 6.6, 6.0, 5.4, 5.9…
$ votes       <int> 348, 20, 5, 6, 17, 45, 200, 24, 18, 51, 23, 53, 44, 11, 12…
$ r1          <dbl> 4.5, 0.0, 0.0, 14.5, 24.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4…
$ r2          <dbl> 4.5, 14.5, 0.0, 0.0, 4.5, 4.5, 0.0, 4.5, 4.5, 0.0, 0.0, 0.…
$ r3          <dbl> 4.5, 4.5, 0.0, 0.0, 0.0, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5…
$ r4          <dbl> 4.5, 24.5, 0.0, 0.0, 14.5, 14.5, 4.5, 4.5, 0.0, 4.5, 14.5,…
$ r5          <dbl> 14.5, 14.5, 0.0, 0.0, 14.5, 14.5, 24.5, 4.5, 0.0, 4.5, 24.…
$ r6          <dbl> 24.5, 14.5, 24.5, 0.0, 4.5, 14.5, 24.5, 14.5, 0.0, 44.5, 4…
$ r7          <dbl> 24.5, 14.5, 0.0, 0.0, 0.0, 4.5, 14.5, 14.5, 34.5, 14.5, 24…
$ r8          <dbl> 14.5, 4.5, 44.5, 0.0, 0.0, 4.5, 4.5, 14.5, 14.5, 4.5, 4.5,…
$ r9          <dbl> 4.5, 4.5, 24.5, 34.5, 0.0, 14.5, 4.5, 4.5, 4.5, 4.5, 14.5,…
$ r10         <dbl> 4.5, 14.5, 24.5, 45.5, 24.5, 14.5, 14.5, 14.5, 24.5, 4.5, …
$ mpaa        <chr> "", "", "", "", "", "", "R", "", "", "", "", "", "", "", "…
$ Action      <int> 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0…
$ Animation   <int> 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1…
$ Comedy      <int> 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1…
$ Drama       <int> 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0…
$ Documentary <int> 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0…
$ Romance     <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ Short       <int> 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1…

Code Style & Structure

The gallery follows a Rule of Least Power Principle, in the sense that “a language with a straightforward syntax may be easier to analyze than an otherwise equivalent one with more complex structure.” In other words, assuming the reader has some understanding of R and the tidyverse, the code for each graph is meant to be read and understood without having to run it.

  • All graph labels have the labs_ prefix

  • All graph layers have a ggp2_ prefix

Each graph has the same four sections and structure: Packages, Data, Code, and Graph

Graph Code Structure

PACKAGES:

Install packages.

View code
library(ggplot2)

DATA:

Description of data

View code
df <- tibble::tibble(X = sample(x = 1:100, 10, FALSE),
                     Y = rlnorm(10, 1, 3))

CODE:

Code for creating graph

View code
# labels
labs_graph <- ggplot2::labs(title = "Title", 
                            subtitle = "subtitle", 
                            x = "X", y = "Y")
# layers
ggp2_graph <- ggplot2::ggplot(data = df, 
    mapping = aes(x = X, y = Y)) + 
    ggplot2::geom_blank()
# graph
ggp2_graph + 
    labs_graph

GRAPH:

I’ve attempted to balance brevity and clarity, but with the assumption that its best to err on the latter. I’ve also followed the general principle that if a graph can be easily built using one of ggplot2 ’s geom_* functions, that method is shown first.

If the graph can’t be built with ggplot2’s geoms and requires additional packages/geoms/functions, instructions for installing the development version for these packages has been provided.

Graph Categories

Graphs have been categorized into the following types:

  • Univariate graphs
  • Amounts
  • Proportions
  • Comparing Distributions
  • Relationships

Some graphs would justifiably belong in more than one category, and wherever this is the cases, I’ve tried to include other uses in the notes.

Univariate Graphs

The following graphs are used to display single variable distributions:

  • Bar Graphs

  • Histograms

  • Frequency Polygons

  • Dot-Plots

  • Density Plots

  • Violin Plots

  • Box-Plots

Amounts

The following graphs are used to display amounts:

  • Grouped column-graphs

  • Summarized column-graphs

  • Overlapping bar-graphs

  • Side-to-side ("dodge") bar-graphs

  • Side-to-side ("dodge2") bar-graphs

  • Waffle charts

    • ggwaffle package

      View code
      # Waffle Charts
      devtools::install_github("liamgilbey/ggwaffle")
      library(ggwaffle)
  • Heatmaps

Proportions

The following graphs are used to compare proportions:

  • Pie Charts

    • ggpubr package

      View code
      # Pie Charts 
      devtools::install_github("kassambara/ggpubr")
      library(ggpubr)
  • Diverging Bar-graphs

  • Vertical

  • Stacked-densities

  • Mosaic Plots

    • ggmosaic package

      View code
      # Mosaic Charts 
      devtools::install_github("haleyjeppson/ggmosaic")
      library(ggmosaic)
  • Treemaps

    • treemapify package

      View code
      # Treemaps
      devtools::install_github("wilkox/treemapify")
      library(treemapify)

Comparing Distributions

These graphs can be used to compare multiple variable distributions:

  • Overlapping Histograms

  • Overlapping Frequency Polygons

  • Overlapping Dot-Plots

  • Bee-swarm Plots

    • ggbeeswarm package

      View code
      # Bee-swarm Plots
      devtools::install_github("eclarke/ggbeeswarm")
      library(ggbeeswarm)
  • Overlapping Densities

  • Ridgeline Plots

    • ggridges package

      View code
      # Ridgeline plots 
      devtools::install_github("wilkelab/ggridges")
      library(ggridges)
  • Grouped Box-plots

  • Violin plots

  • Rain-cloud plots

    • raincloudplots and ggdist packages

      View code
      # Rain-cloud plots 
      remotes::install_github('jorvlan/raincloudplots')
      remotes::install_github('mjskay/ggdist')
      library(raincloudplots)
      library(ggdist)

Relationships

The following graphs are used to display relationships between two (or more) variables:

  • Scatter plots

  • Grouped scatter plots

  • Bubble charts

  • Grouped bubble charts

  • Alluvial charts

    • ggalluvial package

      View code
      # Alluvial charts
      devtools::install_github("corybrunson/ggalluvial")
      library(ggalluvial)
  • Bump charts

    • ggbump package

      View code
      # Bump charts
      devtools::install_github("davidsjoberg/ggbump")
      library(ggbump)
  • Parallel Sets

    • ggforce package

      View code
      # Bump charts
      devtools::install_github("thomasp85/ggforce")
      library(ggforce)
  • Slope graphs

  • Density Contours

  • 2D Histograms

  • Hex Bins

  • Correlograms